package com.example.myapplication.cpdc;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: rongfeng
 * @date: 2021/8/25
 * @description
 */
public class ReadJson {
    public static void main(String[] args) {
        List<BookWordBean> list = new ArrayList<>();
        try {
            FileInputStream inputStream = new FileInputStream("C:\\Users\\demo\\Downloads\\1521164657744_IELTS_2\\IELTS_2.json");
            InputStreamReader inputreader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader buffreader = new BufferedReader(inputreader);
            String line = "";
            int count = 0;
            while ((line = buffreader.readLine()) != null) {
                count = count + 1;
                BookWordBean wordBean = GsonUtil.parse(line, BookWordBean.class);
                list.add(wordBean);
            }
            inputStream.close();//关闭输入流

            StringBuilder stringBuilder = new StringBuilder();
            for (BookWordBean item : list
            ) {
                System.out.println(item.getHeadWord());
                stringBuilder.append(item.getHeadWord()).append("\n");
            }

            File file = new File("C:\\Users\\demo\\Downloads\\1521164657744_IELTS_2\\IELTS_2.txt");
            try {
                if (file.exists()) {
                    file.delete();
                    file.createNewFile();
                }
                BufferedWriter out = new BufferedWriter(new FileWriter(file));
                out.write(stringBuilder.toString());
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

//            for (BookWordBean item : list) {
//                File file1 = new File("D:\\word\\all\\" + item.getHeadWord() + ".json");
//                if (file1.exists()) {
//                    redFile(file1, item.getHeadWord());
//                }
//            }
        } catch (Exception e) {
        }

    }

    public static void redFile(File file, String name) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            StringBuilder stringBuilder = new StringBuilder();
            while ((text = bufferedReader.readLine()) != null) {
                stringBuilder.append(text + "\n");
            }
            fileInputStream.close();//关闭输入流
            saveFile(stringBuilder.toString(), new File("D:\\word\\IELTS_2\\" + name + ".json"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveFile(String string, File file) {
        try {
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(string);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("保存完成 ");
    }


}
